package com.user.application.query.assembler;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.user.application.query.dto.UserDto;
import com.user.infrastructure.entity.SignDao;
import com.user.infrastructure.entity.UserDao;
import com.user.infrastructure.repository.mapper.SignMapper;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.user.domain.factory.UserFactory.USER_DAO_CONVERT;


/**
 * <p>
 * 负责组装用户信息
 * </p>
 *
 * @author 大忽悠
 * @create 2022/9/28 18:38
 */
@Component
@RequiredArgsConstructor
public class UserDtoAssembler {
    private final SignMapper signMapper;

    /**
     * 根据传入的UserDao信息,组装返回UserDto
     */
    public List<UserDto> apply(List<UserDao> userDaos) {
        Map<Integer, List<SignDao>> signGroupByUserId = signMapper.selectList(new QueryWrapper<SignDao>().in("user_id",userDaos.stream().map(UserDao::getId).collect(Collectors.toList())))
                .stream().collect(Collectors.groupingBy(SignDao::getUserId));
        return USER_DAO_CONVERT.toUserDtoList(userDaos).stream().map(userDto -> {
            userDto.setSignInNum(signGroupByUserId.get(userDto.getId()).get(0).getSignNum());
            return userDto;
        }).collect(Collectors.toList());
    }
}
